﻿using SalesSys.DAL;
using SalesSys.DAL.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SalesSys.BLL
{
    public class NowDepotProfitReportBLL
    {
        /// <summary>
        /// 日经营分析报表-库存权益-获取品牌统计
        /// </summary>
        /// <returns></returns>
        public List<NowDepotProfitReport> GetSupplierReportList()
        {
            string sql = "select NowDepotProfitReport.厂家ID,NowDepotProfitReport.厂家,SUM(NowDepotProfitReport.定价未售数量) as 定价未售数量,sum(NowDepotProfitReport.存货) as 库存数量,sum(case when NowDepotProfitReport.在途数量 is null then 0 else NowDepotProfitReport.在途数量 end) as 在途数量,sum(case when NowDepotProfitReport.成本均价 is null then 0 else NowDepotProfitReport.成本均价 end) as 库存均价,sum(NowDepotProfitReport.销售均价) as 售价,sum(case when NowDepotProfitReport.价差 is null then 0 else NowDepotProfitReport.价差 end) as 利差,sum((case when NowDepotProfitReport.总金额 is null then 0 else NowDepotProfitReport.总金额 end)-(case when NowDepotProfitReport.总运费 is null then 0 else NowDepotProfitReport.总运费 end)) as 毛利额,sum((case when NowDepotProfitReport.存货 is null then 0 else NowDepotProfitReport.存货 end)+(case when NowDepotProfitReport.在途数量 is null then 0 else NowDepotProfitReport.在途数量 end )-(case when NowDepotProfitReport.定价未售数量 is null then 0 else NowDepotProfitReport.定价未售数量 end)) as 出厂未定数量 from Sal_Supplier ";
            sql += " inner join NowDepotProfitReport on NowDepotProfitReport.厂家ID=Sal_Supplier.Id";
            sql += " group by NowDepotProfitReport.厂家ID,NowDepotProfitReport.厂家 ";
            var rep = RepositoryFactory.GetRepositoryByFactoryUnit<IDepotHistroyRep>();
            var dt = rep.GetTableInfo(sql);
            return FillModel(dt);
        }

        /// <summary>
        /// 日经营分析报表-库存权益-根据厂家进行统计
        /// </summary>
        /// <param name="supplierId">厂家ID</param>
        /// <returns></returns>
        public List<NowDepotProfitReport> GetNowDepotProfitReportList(Guid? supplierId)
        {
            string sql = "select NowDepotProfitReport.仓库ID,NowDepotProfitReport.厂家ID,NowDepotProfitReport.厂家,NowDepotProfitReport.仓库,NowDepotProfitReport.定价未售数量,NowDepotProfitReport.存货 库存数量,(case when NowDepotProfitReport.在途数量 is null then 0 else NowDepotProfitReport.在途数量 end) as 在途数量,(case when NowDepotProfitReport.成本均价 is null then 0 else NowDepotProfitReport.成本均价 end) 库存均价,(case when NowDepotProfitReport.销售均价 is null then 0 else NowDepotProfitReport.销售均价 end) 售价,(case when NowDepotProfitReport.价差 is null then 0 else NowDepotProfitReport.价差 end) 利差,((case when NowDepotProfitReport.总金额 is null then 0 else NowDepotProfitReport.总金额 end)- (case when NowDepotProfitReport.总运费 is null then 0 else NowDepotProfitReport.总运费 end))毛利额,((case when NowDepotProfitReport.存货 is null then 0 else NowDepotProfitReport.存货 end)+(case when NowDepotProfitReport.在途数量 is null then 0 else NowDepotProfitReport.在途数量 end)-(case when NowDepotProfitReport.定价未售数量 is null then 0 else NowDepotProfitReport.定价未售数量 end)) 出厂未定数量 from NowDepotProfitReport ";
            sql += "  where 1=1 ";
            if (supplierId.HasValue)
            {
                sql += " and 厂家ID= '" + supplierId + "'";
            }
            var rep = RepositoryFactory.GetRepositoryByFactoryUnit<IDepotHistroyRep>();
            var dt = rep.GetTableInfo(sql);
            return FillModel(dt);
        }

        /// <summary>  
        /// 填充对象列表：用DataTable填充实体类
        /// </summary>  
        public List<NowDepotProfitReport> FillModel(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return new List<NowDepotProfitReport>();
            }
            List<NowDepotProfitReport> modelList = new List<NowDepotProfitReport>();
            foreach (DataRow dr in dt.Rows)
            {
                NowDepotProfitReport model = new NowDepotProfitReport();
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                    if (propertyInfo != null && dr[i] != DBNull.Value)
                        propertyInfo.SetValue(model, dr[i], null);
                }

                modelList.Add(model);
            }
            return modelList;
        }
    }
}
